<template>
  <div class="article-container">
    <el-card class="filter-card">
      <div slot="header">
        <!-- 面包屑路径导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- /面包屑路径导航 -->
      </div>
      <!-- 数据筛选表单 -->
      <el-form ref="form" :model="form" label-width="40px" size="mini">
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <!-- 如果选择了全部，则将都设置为null,在发请求时，axios就是会自动忽略这个参数
            就不向后端传递 -->
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>

        <!--频道组件-->
        <my-channel v-model="form.channelId"></my-channel>

        <el-form-item label="日期">
          <el-date-picker
            v-model="form.dateRange"
            type="datetimerange"
            range-separator="至"
            value-format="yyyy-MM-dd"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="hQuery" :loading="isLoading">查询</el-button>
        </el-form-item>
      </el-form>
      <!-- /数据筛选表单 -->
    </el-card>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        根据筛选条件共查询到 {{total_count}} 条结果：
      </div>
      <!-- 数据列表 -->
        <el-table
        v-loading="isLoading"
        :data="articles"
        style="width: 100%">
        <el-table-column
          prop="date"
          label="封面"
          >
          <template slot-scope="scope">
           <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.cover.images[0]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column
          prop="title"
          label="标题"
          >
        </el-table-column>
        <el-table-column
          prop="status"
          label="状态">
          <!-- 发布文章的状态 -->
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 0" type="info">草稿</el-tag>
            <el-tag v-else-if="scope.row.status===1" type="success">待审核</el-tag>
            <el-tag v-else-if="scope.row.status===2">审核通过</el-tag>
            <el-tag v-else-if="scope.row.status===3" type="warning">审核失败</el-tag>
            <el-tag v-else-if="scope.row.status===4" type="danger">已删除</el-tag>
            <el-tag v-else type="danger">unknow</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="pubdate"
          label="发布时间">
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <!--
              scope.row:    表示这一行的数据
              scope.$index: 表示这一行的索引
            -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="hEdit(scope.$index, scope.row)">
            </el-button>

            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="hDelete(scope.$index, scope.row)">
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- /数据列表 -->

      <!-- 列表分页 -->
      <el-pagination
        :disabled="isLoading"
        layout="prev, pager, next"
        background
        :page-size="10"
        @current-change="hPageChange"
        :total="total_count">
      </el-pagination>
      <!-- /列表分页 -->
    </el-card>
  </div>
</template>

<script>
import MyChannel from '@/components/myChannel.vue'
export default {
  name: 'ArticleIndex',
  components: {
    MyChannel
  },
  props: {},
  data () {
    return {
      isLoading: false,
      page: 1, // 当前要查询的页码
      total_count: 0,
      articles: [
        // {cover:Object,id,pubdate,status,title}
      ], // 文章列表
      form: {
        status: null, // 与全部相对应,文章状态
        channelId: null, // 如果初始值为null,则axios不会传这个参数
        dateRange: null // 日期时间区间
      }
    }
  },
  computed: {},
  watch: {},
  // created () {},
  mounted () {
    this.doQuery()
  },
  methods: {
    // 建立参数
    buildParam () {
      // 解构赋值
      const { status, dateRange, channelId } = this.form
      const paramsObj = {
        page: this.page
      }
      // 检查状态
      // 对于axios来说,它有一个特点,如果你发现你要传的参数值是null,则它会省略这个参数(不传这个参数)
      paramsObj.status = status
      paramsObj.channel_id = channelId
      // 检查处理日期时间
      if (dateRange) {
        paramsObj.begin_pubdate = dateRange[0]
        paramsObj.end_pubdate = dateRange[1]
      }
      return paramsObj
    },
    // 发请求数据
    async doQuery () {
      //  阅读接口文档,做填空题
      // 1.组装参数
      const params = this.buildParam()
      this.isLoading = true
      try {
        // 2.带上参数,发请求
        const res = await this.$axios({
          method: 'GET',
          url: '/mp/v1_0/articles',
          // 参数
          params
        })
        console.log(res)
        // 3.更新数据
        this.articles = res.data.data.results
        this.total_count = res.data.data.total_count
        // 4.本次查询结束
        this.isLoading = false
        // console.log(res)
      } catch (err) {
        this.$message.error('查询出错')
        // 更新数据
        this.articles = []
        this.total_count = 0
        // 本次查询结束
        this.isLoading = false
      }
    },
    hPageChange (curPage) {
      console.log('点击了分页', curPage)
      // 1.更新页码
      this.page = curPage
      // 重发请求
      this.doQuery()
    },
    onSubmit () {
      console.log('submit!')
    },
    hQuery () {
      this.doQuery()
    },
    hEdit (index, row) {
      console.log(index, row)
    },
    async doDelete (id) {
      try {
        const res = await this.$axios({
          methods: 'DELETE',
          url: '/mp/v1_0/articles/' + id
        })
        console.log(res)
      } catch (err) {
        this.$message.error('删除失败')
        console.log(err)
      }
    },
    // 用户删除文章 row表示当前行的数据
    hDelete (index, row) {
      // 对于用户的退出,数据的删除操作,一般要询问一下(确认对话框)
      this.$confirm('你真的要删除吗?').then(() => {
        // 这里的id值已经是大数处理之后的对象,所以要恢复数据出来
        // 要用toString()
        const id = row.id.toString()
        console.log('你要删除的数据是', id)
        this.doDelete(id)
      }).catch(() => {
        // 取消
      })
      console.log(index, row)
    }
  }
}

</script>

<style scoped lang="less">
.filter-card {
  margin-bottom: 30px;
}

.list-table {
  margin-bottom: 20px;
}
</style>
